<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p id="pt"></p>
<p id="user1"></p>
<p id="user2"></p>
<p id="ans1"></p>
<button id="shitou" onclick="shitou()">石头</button>
<button id="jiandao" onclick="jiandao()">剪刀</button>
<button id="bu" onclick="bu()">布</button>
</body>
<script>
    //用JavaScript操控DOM画出角色
    //如果只有一个角色就先不画，就每隔2秒弹出个等待玩家加入
    const {session} = require('electron').remote;

    let tsss = 0;
    let ysss = 45645;
    let gameNum = 9999;
    let qwer = 0;
    session.defaultSession.cookies.get({ url: 'http://localhost/home.html' }, (error, cookies) => {
        tsss = cookies[0].name;
    });
    let timer = setInterval(function () {
        select();
        if(qwer == 0) {
            if (document.getElementById('user2').innerHTML == ysss) {
                clearInterval(timer2);
                console.log('time2结束');
                qwer = 1;
            }
        }
    }, 2000);
    let timer1 = setInterval(function () {
        for (let t = 0; t < global.qqw.length; t++) {
            if (gameNum == global.qqw[t].num) {
                if (global.qqw[t].user1 == "shitou" || global.qqw[t].user1 == "jiandao" || global.qqw[t].user1 == "bu") {
                    if (global.qqw[t].user2 == "shitou" || global.qqw[t].user2 == "jiandao" || global.qqw[t].user2 == "bu") {
                        if(global.qqw[t].user1 == "shitou"){
                            if(global.qqw[t].user2 == "bu"){
                                document.getElementById('ans1').innerHTML = '结果为:玩家' + global.qqw[t].user2 + '获胜';
                            }
                            else if(global.qqw[t].user2 == "jiandao"){
                                document.getElementById('ans1').innerHTML = '结果为:玩家' + global.qqw[t].user1 + '获胜';;
                            }
                            else {
                                document.getElementById('ans1').innerHTML = '结果为：平局';
                            }
                        }
                    }
                    else {
                        document.getElementById('ans1').innerHTML = '等待玩家2';
                    }
                }
                else {
                    document.getElementById('ans1').innerHTML = '等待双方玩家选择';
                }

            }
        }
    },2000);
    let timer2 = setInterval(function () {
        for(let t = 0;t < global.qqw.length; t++) {
            // console.log(global.qqw[t].user1);
            if(global.qqw[t].num != 0 && (global.qqw[t].user1 == tsss || global.qqw[t].user2 == tsss)){
                if(global.qqw[t].user2 != 999){
                    gameNum = global.qqw[t].num;
                    document.getElementById('user1').innerHTML = global.qqw[t].user1;
                    document.getElementById('user2').innerHTML = global.qqw[t].user2;
                    ysss = global.qqw[t].user2;
                }
                else{
                    gameNum = global.qqw[t].num;
                    document.getElementById('user1').innerHTML = global.qqw[t].user1;
                    document.getElementById('user2').innerHTML = "等待玩家加入";
                }
            }
        }
    }, 2000);

    function shitou() {
        for(let t = 0; t < global.qqw.length; t++){
            if((global.qqw[t].user1 == tsss || global.qqw[t].user1 == tsss) && global.qqw[t].num == gameNum){
                if(global.qqw[t].user1 == tsss){
                    updateGame1(global.qqw[t].num,'shitou');
                }
                else{
                    updateGame2(global.qqw[t].num,'shitou');
                }
            }
        }
    }
    function jiandao() {
        for(let t = 0; t < global.qqw.length; t++){
            if((global.qqw[t].user1 == tsss || global.qqw[t].user1 == tsss) && global.qqw[t].num == gameNum){
                if(global.qqw[t].user1 == tsss){
                    updateGame1(global.qqw[t].num,'jiandao');
                }
                else{
                    updateGame2(global.qqw[t].num,'jiandao');
                }
            }
        }
    }
    function bu() {
        for(let t = 0; t < global.qqw.length; t++){
            if((global.qqw[t].user1 == tsss || global.qqw[t].user1 == tsss) && global.qqw[t].num == gameNum){
                if(global.qqw[t].user1 == tsss){
                    updateGame1(global.qqw[t].num,'bu');
                }
                else{
                    updateGame2(global.qqw[t].num,'bu');
                }
            }
        }
    }
    function select() {
        let mysql  = require('mysql');
        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });

        connection.connect();

        let  sql = 'SELECT * FROM game';
        //查
        connection.query(sql,function (err, result) {
            if(err){
                console.log('[SELECT ERROR] - ',err.message);
                return;
            }
            global.qqw = result;
        });
        connection.end();
    }
    function add(num,username1,username2) {
        let mysql  = require('mysql');
        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });
        connection.connect();
        let addSql = 'INSERT INTO game(num,user1,user2) VALUES(?,?,?)';
        let addSqlParams = [];
        addSqlParams.push(num);
        addSqlParams.push(username1);
        addSqlParams.push(username2);
//增
        connection.query(addSql,addSqlParams,function (err, result) {
            if(err){
                console.log('[INSERT ERROR] - ',err.message);
                return;
            }
        });
        connection.end();
    }
    function updateGame1(num,username1) {
        let mysql  = require('mysql');
        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });
        connection.connect();
        let modSql = 'UPDATE game SET user1 = ? WHERE num = ?';
        let modSqlParams = [];
        modSqlParams.push(username1);
        modSqlParams.push(num);
        console.log(modSqlParams);
//改
        connection.query(modSql,modSqlParams,function (err, result) {
            if(err){
                console.log('[UPDATE ERROR] - ',err.message);
                return;
            }
            console.log(result);
        });
        connection.end();
    }
    function updateGame2(num,username2) {
        let mysql  = require('mysql');
        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });
        connection.connect();
        let modSql = 'UPDATE game SET user2 = ? WHERE num = ?';
        let modSqlParams = [];
        modSqlParams.push(username2);
        modSqlParams.push(num);
        console.log(modSqlParams);
//改
        connection.query(modSql,modSqlParams,function (err, result) {
            if(err){
                console.log('[UPDATE ERROR] - ',err.message);
                return;
            }
            console.log(result);
        });
        connection.end();
    }
</script>
</html>